package com.example.iccil.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.iccil.annotation.AuthCheck;
import com.example.iccil.common.BaseResponse;
import com.example.iccil.common.ErrorCode;
import com.example.iccil.common.ResultUtils;
import com.example.iccil.constant.UserConstant;
import com.example.iccil.exception.BusinessException;
import com.example.iccil.exception.ThrowUtils;
import com.example.iccil.model.dto.space.SpaceAddRequest;
import com.example.iccil.model.dto.space.SpaceEditRequest;
import com.example.iccil.model.dto.space.SpaceQueryRequest;
import com.example.iccil.model.dto.space.SpaceUpdateRequest;
import com.example.iccil.model.entity.Space;
import com.example.iccil.model.entity.User;
import com.example.iccil.model.enums.SpaceLevelEnum;
import com.example.iccil.model.vo.SpaceLevel;
import com.example.iccil.model.vo.SpaceVO;
import com.example.iccil.service.SpaceService;
import com.example.iccil.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yolo
 * @Program yolo-picture
 * @description
 * @create 2025-06-27 11:05
 **/
@RestController()
@RequestMapping("/space")
public class SpaceController {

    @Resource
    private SpaceService spaceService;
    @Resource
    private UserService userService;

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest spaceUpdateRequest) {
        if (spaceUpdateRequest == null || spaceUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 将实体类和 DTO 进行转换
        Space space = new Space();
        BeanUtils.copyProperties(spaceUpdateRequest, space);
        // 自动填充数据
        spaceService.fillSpaceBySpaceLevel(space);
        // 数据校验
        spaceService.validSpace(space, false);
        // 判断是否存在
        long id = spaceUpdateRequest.getId();
        Space oldSpace = spaceService.getById(id);
        ThrowUtils.throwIf(oldSpace == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = spaceService.updateById(space);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @GetMapping("/list/level")
    public BaseResponse<List<SpaceLevel>> listSpaceLevel() {
        List<SpaceLevel> spaceLevelList = Arrays.stream(SpaceLevelEnum.values()) // 获取所有枚举
                .map(spaceLevelEnum -> new SpaceLevel(
                        spaceLevelEnum.getValue(),
                        spaceLevelEnum.getText(),
                        spaceLevelEnum.getMaxCount(),
                        spaceLevelEnum.getMaxSize()))
                .collect(Collectors.toList());
        return ResultUtils.success(spaceLevelList);
    }

    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Space>> listSpaceByPage(@RequestBody SpaceQueryRequest spaceQueryRequest) {
        long current = spaceQueryRequest.getCurrent();
        long size = spaceQueryRequest.getPageSize();
        // 查询数据库
        Page<Space> spacePage = spaceService.page(new Page<>(current, size),
                spaceService.getQueryWrapper(spaceQueryRequest));
        return ResultUtils.success(spacePage);
    }

    @PostMapping("/list/voPage")
    @AuthCheck(mustRole = UserConstant.DEFAULT_ROLE)
    public BaseResponse<Page<SpaceVO>> listSpaceVoByPage(@RequestBody SpaceQueryRequest spaceQueryRequest) {
        long current = spaceQueryRequest.getCurrent();
        long size = spaceQueryRequest.getPageSize();
        // 查询数据库
        Page<Space> spacePage = spaceService.page(new Page<>(current, size),
                spaceService.getQueryWrapper(spaceQueryRequest));
        List<SpaceVO> spaceVOList = spacePage.getRecords().stream().map(SpaceVO::objToVo).collect(Collectors.toList());
        Page<SpaceVO> spaceVOPage = new Page<SpaceVO>().setRecords(spaceVOList).setCurrent(spacePage.getCurrent()).setTotal(spacePage.getTotal()).setSize(spacePage.getSize());
        return ResultUtils.success(spaceVOPage);
    }

    @PostMapping("/addSpace")
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest addRequest, HttpServletRequest request){
        ThrowUtils.throwIf(addRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        long spaceId = spaceService.addSpace(addRequest, loginUser);
        return ResultUtils.success(spaceId);
    }

    @PostMapping("/editSpace")
    public void editSpace(@RequestBody SpaceEditRequest spaceEditRequest, HttpServletRequest request){
        ThrowUtils.throwIf(spaceEditRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        return;
    }

    @GetMapping("getSpaceVoById")
    @AuthCheck(mustRole = UserConstant.DEFAULT_ROLE)
    public BaseResponse<SpaceVO> getSpaceVoById(@RequestParam long id){
        ThrowUtils.throwIf(ObjUtil.isEmpty(id), ErrorCode.PARAMS_ERROR);
        Space space = spaceService.getById(id);
        SpaceVO spaceVO = SpaceVO.objToVo(space);
        return ResultUtils.success(spaceVO);
    }
    @GetMapping("/doc1")
    public void doc1(@RequestBody SpaceAddRequest spaceAddRequest){
        return;
    }
    @GetMapping("/doc2")
    public void doc2(@RequestBody SpaceEditRequest spaceEditRequest){
        return;
    }
    @GetMapping("/doc3")
    public void doc3(@RequestBody SpaceQueryRequest spaceQueryRequest){
        return;
    }
}
